package cn.com.taiji.pos.mapper;

import cn.com.taiji.pos.entity.ItemS;
import cn.com.taiji.pos.entity.TradeConsumptionDetail;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDate;
import java.util.List;

/**
 * <p>
 * 消费细单 Mapper 接口
 * </p>
 *
 * @author
 * @since 2021-03-01
 */
public interface TradeConsumptionDetailMapper extends BaseMapper<TradeConsumptionDetail> {

    @Select("        SELECT\n" +
            "        mft.ID as foodId,\n" +
            "        mft.NAME_ZH as nameZh,\n" +
            "        mft.NAME_EN as nameEn,\n" +
            "        mft.`CODE` AS posNo,\n" +
            "        tcd.PRICE as price,\n" +
            "        tcd.QUANTITY as count,\n" +
            "        tcd.ACTUAL_AMOUNT as amount\n" +
            "        FROM\n" +
            "        trade_consumption_detail tcd\n" +
            "        LEFT JOIN trade_consumption_main tcm ON tcm.ID = tcd.MAIN_ID\n" +
            "        LEFT JOIN menu_food_item mft ON mft.ID = tcd.FOOD_ID\n" +
            "        LEFT JOIN menu_food_category mfc ON mfc.ID = mft.FOOD_CATEGORY_ID\n" +
            "        WHERE\n" +
            "        DATE_FORMAT(tcm.CONSUMPTION_TIME,'%Y-%m-%d') =\"${startDate}\" AND\n" +
            "        mfc.`CODE`=\"PF115\"\n" +
            "        ORDER BY tcm.CONSUMPTION_TIME DESC")
    List<ItemS> getSalesOrder(@Param("startDate") LocalDate searchDate);


    @Select("        SELECT\n" +
            "        mft.ID as foodId,\n" +
            "        mft.NAME_ZH as nameZh,\n" +
            "        mft.NAME_EN as nameEn,\n" +
            "        tcd.PRICE as price,\n" +
            "        tcd.QUANTITY as count,\n" +
            "        tcd.ACTUAL_AMOUNT as amount\n" +
            "        FROM\n" +
            "        trade_consumption_detail tcd\n" +
            "        LEFT JOIN trade_consumption_main tcm ON tcm.ID = tcd.MAIN_ID\n" +
            "        LEFT JOIN menu_food_item mft ON mft.ID = tcd.FOOD_ID\n" +
            "        LEFT JOIN menu_food_category mfc ON mfc.ID = mft.FOOD_CATEGORY_ID\n" +
            "        WHERE\n" +
            "        DATE_FORMAT(tcm.CONSUMPTION_TIME,'%Y-%m-%d') =\"${startDate}\" AND\n" +
            "        mfc.`CODE`=\"PF115\"\n" +
            "        ORDER BY tcm.CONSUMPTION_TIME DESC")
    IPage<ItemS> getSalesOrderPage(IPage<ItemS> page, @Param("startDate") LocalDate searchDate);


    @Select("SELECT\n" +
            "\tSUM( tcd.ACTUAL_AMOUNT ) AS totalAmount \n" +
            "FROM\n" +
            "\ttrade_consumption_detail tcd\n" +
            "\tLEFT JOIN trade_consumption_main tcm ON tcm.ID = tcd.MAIN_ID\n" +
            "\tLEFT JOIN menu_food_item mft ON mft.ID = tcd.FOOD_ID\n" +
            "\tLEFT JOIN menu_food_category mfc ON mfc.ID = mft.FOOD_CATEGORY_ID \n" +
            "\tLEFT JOIN trade_account_consumption_flow tacf ON tcm.FLOW_NO = tacf.FLOW_NO \n" +
            "WHERE\n" +
            "\tDATE_FORMAT(tcm.CONSUMPTION_TIME,'%Y-%m-%d') =\"${startDate}\" \n" +
            "\tAND tacf.IS_OVER = '1' \n" +
            "\tAND mfc.`CODE` = \"PF115\"")
    Double getDaySalesTotalAmount(@Param("startDate") LocalDate searchDate);


    @Select("SELECT\n" +
            "\tmft.ID AS foodId,\n" +
            "\tmft.NAME_ZH AS nameZh,\n" +
            "\tmft.NAME_EN AS nameEn,\n" +
            "\tmft.`CODE` AS code,\n" +
            "\ttcd.PRICE AS price,\n" +
            "\ttcd.IS_CREATE_INVENTORY AS isCreateInventory,\n" +
            "\tSUM(tcd.QUANTITY)\t AS count,\n" +
            "\tSUM(tcd.QUANTITY)\t AS quantity,\n" +
            "\tSUM(tcd.ACTUAL_AMOUNT) AS amount \n" +
            "FROM\n" +
            "\ttrade_consumption_detail tcd\n" +
            "\tLEFT JOIN trade_consumption_main tcm ON tcm.ID = tcd.MAIN_ID\n" +
            "\tLEFT JOIN menu_food_item mft ON mft.ID = tcd.FOOD_ID\n" +
            "\tLEFT JOIN menu_food_category mfc ON mfc.ID = mft.FOOD_CATEGORY_ID \n" +
            "\tLEFT JOIN trade_account_consumption_flow tacf ON tcm.FLOW_NO = tacf.FLOW_NO \n" +
            "WHERE\n" +
            "\tDATE_FORMAT(tcm.CONSUMPTION_TIME,'%Y-%m-%d') = \"${startDate}\" \n" +
            "\tAND mfc.`CODE` = \"PF115\" \n" +
            "\tAND tacf.IS_OVER = '1' \n" +
            "\tAND tcd.IS_CREATE_INVENTORY = ${isCreateInventory} \n" +
            "\tGROUP BY mft.ID\n" +
            "ORDER BY\n" +
            "\ttcm.CONSUMPTION_TIME DESC ")
    List<ItemS> getSalesOrderGroupByFood(@Param("startDate") LocalDate searchDate, @Param("isCreateInventory") int isCreateInventory);


}
